package com.dfbz.controller.user;

import com.dfbz.controller.BaseController;
import com.dfbz.model.User;
import com.dfbz.service.UserService;
import com.dfbz.service.impl.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/user/*")
public class UserController extends BaseController {

    private UserService userService=new UserServiceImpl();

    public void login(HttpServletRequest req, HttpServletResponse resp)  {
        try {
            String username = req.getParameter("uname");
            String password = req.getParameter("pwd");
            User user = userService.findByName(username);
            System.out.println(user);
            if (null!=user){
                if (password.equals(user.getPassword())){
                    resp.sendRedirect(req.getContextPath()+"/html/home.html");
                    return;
                }else {
                    req.getSession().setAttribute("loginMsg","密码错误");
                }
            }else {
                req.getSession().setAttribute("loginMsg","找不到该用户");
            }
            resp.sendRedirect(req.getContextPath()+"/index.jsp");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void register(HttpServletRequest req, HttpServletResponse resp){
        try {
            String email = req.getParameter("email");
            String username = req.getParameter("username");
            String password = req.getParameter("password");
            String password2 = req.getParameter("password2");
            boolean b = userService.checkByEmailorName(email,username);
            if (!b){
                //不可以注册
                req.getSession().setAttribute("registerMsg","该用户或者邮箱已经被注册");
                resp.sendRedirect(req.getContextPath()+"/register.jsp");
            }else {
                //可以注册
                if (password.equals(password2)){
                    User user = new User();
                    user.setEmail(email);
                    user.setUsername(username);
                    user.setPassword(password);
                    userService.addUser(user);
                }else {
                    req.getSession().setAttribute("registerMsg","两次密码不一致");
                    resp.sendRedirect(req.getContextPath()+"/register.jsp");
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    public void checkEmail(HttpServletRequest req, HttpServletResponse resp){
        try {
            String email = req.getParameter("email");
            User user=userService.findByEmail(email);
            String data="0";
            if (null!=user){
                data ="1";
            }
            resp.getWriter().write(data);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void checkUser(HttpServletRequest req, HttpServletResponse resp){
        try {
            String username = req.getParameter("username");
            User user=userService.findByName(username);
            String data="0";
            if (null!=user){
                data ="1";
            }
            resp.getWriter().write(data);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


}
